<?php namespace Admin\Controller;
/*
 * 产品控制器
 * */
 	class Goodscontroller extends CommonController
 	{
 		protected $cate;
		protected $brand;//实例化品牌模型
		protected $commodity;
		protected $goodsattribute;
		public function __init(){
			$this->cate = new \Common\Model\category;
			$this->brand = new \Common\Model\brand;
			$this->commodity = new \Common\Model\commodity;
			$this->goodsattribute = new \Common\Model\goodsattribute;
		}
 		/********************************************
		 * 产品列表
		 * *******************************************/
		public function index()
		{
			$data = $this->commodity->get();
			
			View::with("data",$data);
			View::make('goods');
		}
		//添加产品
		public function addgoods()
		{
			//获得的所有品牌
			$AllBrand = Db::table("brand")->get();
			View::with("AllBrand",$AllBrand);
			//获得所有分类
			$AllCate = Db::table("category")->get();
			$AllCate = Data::tree($AllCate,"cname");
			View::with("AllCate",$AllCate);
			if(IS_POST){
				//p($_POST);die;
				if(!$this->commodity->addcom()){
					View::error($this->commodity->getError());
				}else if(!$this->goodsattribute->addData()){
					View::error($this->goodsattribute->getError());
				}else{
					View::success("添加成功",U('index'));
				}
				
			}
			
			View::make();
		}
		
		
		
		
		
		
		
		
		
		/************************************
		 * 产品分类
		 * ********************************/
		//分类列表
		public function cate()
		{
			$data = $this->cate->join("type","type_tid","=","tid")->get();
			foreach($data as $k=>$v){
				if($v["pid"]==0){
				$data[$k]['pname']="一级分类";
			}else{
				$pid = $data[$k]["pid"];
				$data[$k]["pname"]=$this->cate->where("cid",$pid)->pluck("cname");
			}
			}
			$data = Data::tree($data,"cname");
			//p($data);die;
			View::with("data",$data);
			View::make();
		}
		
		//添加分类
		public function addcate()
		{
			//获得类别
			$type = Db::table("type")->get();
			//p($type);die;
			View::with("type",$type);
			if(IS_POST){
				if(!$this->cate->addcate()){
					View::error($this->cate->getError());
				}else{
					View::success("添加成功",U('cate'));
				}
			}
			
			View::make();
		}
		
		/*
		 * 添加子类
		 * 
		 * */
		public function addSonCate()
		{
			//获得页面分类
			//1.获得父类id
			$cid = Q("get.cid",0,"intval");
			$cname = $this->cate->where("cid",$cid)->first();
			View::with("cname",$cname);
			//2.获得类型
			$type = Db::table("type")->get();
			View::with("type",$type);
			if(IS_POST){
				if(!$this->cate->addCate()){
					View::error($this->cate->getError());
				}else{
					View::success("添加成功",U('cate'));
				}
			}
			View::make();
		}
		/*
		 * 编辑分类
		 * */
		public function resetCate()
		{
			//原信息
			//1获得id
			$cid = Q("get.cid",0,"intval");
			//2查询
			$olddata = $this->cate
					->join("type","type_tid","=","tid")
					->where("cid",$cid)
					->first();
			if($olddata["pid"]==0){
				$olddata['pname']="一级分类";
			}else{
				$pid = $olddata["pid"];
				$olddata["pname"]=$this->cate->where("cid",$pid)->pluck("cname");
			}		
			//p($olddata);die;	
			View::with("olddata",$olddata);	
			$type = Db::table("type")->get();
			View::with("type",$type);
			//分类
			$cate = $this->cate->getcate($cid);
			$cate = Data::tree($cate,'cname');
			//p($cate);die;
			View::with("cate",$cate);
			
			//进行编辑
			if(IS_POST){
				if(!$this->cate->resetCate($cid)){
					View::error($this->cate->getError());
				}else{
					View::success("编辑成功",U('cate'));
				}
			}
			View::make();
		}
		//删除分类
		public function delcate(){
			$cid = Q("get.cid",0,"intval");
			if(!$this->cate->del($cid)){
				View::error($this->cate->getError());
			}else{
				View::success("删除成功",U('cate'));
			}
		}
		/*****************************************
		 * 品牌
		 * 
		 * ******************************************/
		 //品牌列表
		 public function brand()
		 {
		 	$data = $this->brand->get();
			View::with("data",$data);
		 	View::make();
		 }
		 //添加品牌
		 public function addbrand()
		 {
		 	if(IS_POST){
		 		if(!$this->brand->addbrand()){
		 			View::error($this->brand->getError());
		 		}else{
		 			View::success("添加成功",U("brand"));
		 		}
		 	}
		 	
		 	View::make();
		 }
		 //编辑品牌
		 public function resetbrand()
		 {
		 	$bid=Q("get.bid",0,"intval");
		 	if(IS_POST){
		 		if(!$this->brand->resetbrand()){
		 			View::error($this->brand->getError());
		 		}else{
		 			View::success("编辑成功",U("brand"));
		 		}
		 	}
			//获得元数据
			$olddata = $this->brand->where("bid",$bid)->first();
			View::with("olddata",$olddata);
			View::make();
		 }
		 //删除品牌
		 public function delbrand(){
		 	$bid=Q("get.bid",0,"intval");
		 	if(!$this->brand->del($bid)){
		 		View::error($this->brand->getError());
		 	}else{
		 		go("brand");
		 	}
		 }
 	}
